package com.cookapp.controller;

import com.cookapp.common.utils.Result;
import com.cookapp.model.dto.LoginDTO;
import com.cookapp.model.dto.SmsLoginDTO;
import com.cookapp.model.dto.UserDTO;
import com.cookapp.model.vo.UserVO;
import com.cookapp.service.SmsLoginService;
import com.cookapp.service.UserService;
import com.cookapp.service.WxLoginService;
import lombok.RequiredArgsConstructor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
    @Autowired
    private final WxLoginService wxLoginService;
    @Autowired
    private final UserService userService;
    @Autowired
    private final SmsLoginService smsLoginService;

    /**
     * 微信小程序登录
     */
    @PostMapping("/wx-login")
    public Result<Map<String, Object>> wxLogin(@Valid @RequestBody LoginDTO loginDTO) {
        String token = wxLoginService.login(loginDTO);
        
        Map<String, Object> result = new HashMap<>();
        result.put("token", token);
        return Result.success(result);
    }
    
    /**
     * 发送短信验证码
     */
    @PostMapping("/send-code")
    public Result<Object> sendSmsCode(@RequestParam @NotBlank(message = "手机号不能为空") 
                                      @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确") String phone) {
        smsLoginService.sendLoginCode(phone);
        return Result.success(null, "验证码发送成功");
    }
    
    /**
     * 手机号验证码登录
     */
    @PostMapping("/sms-login")
    public Result<Map<String, Object>> smsLogin(@Valid @RequestBody SmsLoginDTO smsLoginDTO) {
        String token = smsLoginService.login(smsLoginDTO);
        
        Map<String, Object> result = new HashMap<>();
        result.put("token", token);
        return Result.success(result);
    }
    
    /**
     * 获取当前登录用户信息
     */
    @GetMapping("/info")
    public Result<UserVO> getUserInfo() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Long userId = Long.parseLong(authentication.getName());
        return Result.success(userService.getUserInfo(userId));
    }
    
    /**
     * 更新用户信息
     */
    @PutMapping("/info")
    public Result<Boolean> updateUserInfo(@Valid @RequestBody UserDTO userDTO) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Long userId = Long.parseLong(authentication.getName());
        userService.updateUserInfo(userId, userDTO);
        return Result.success(true);
    }
    
    /**
     * 上传用户头像
     */
    @PostMapping("/avatar")
    public Result<String> uploadAvatar(@RequestParam String avatarUrl) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Long userId = Long.parseLong(authentication.getName());
        userService.uploadAvatar(userId, avatarUrl);
        return Result.success(avatarUrl);
    }
    
    /**
     * 获取用户统计数据
     */
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getUserStatistics() {
        // TODO: 实现获取用户统计数据逻辑
        return Result.success(null);
    }
} 